<template>
  <div style="padding: 50px">
    <el-form
      :model="form"
      ref="form"
      label-width="180px"
      style="width: 80%; max-width: 900px"
    >
      <el-form-item
        label="单位名称"
        label-position="left"
        prop="orgName"
        :rules="[
          { required: true, message: '请输入单位名称', trigger: 'blur' },
        ]"
      >
        <el-input v-model="form.orgName"></el-input>
      </el-form-item>
      <!-- <el-form-item
        label="上级单位（总公司）名称"
        label-position="left"
        prop="orgParent"
        :rules="[
          {
            required: true,
            message: '请输入上级单位（总公司）名称',
            trigger: 'change',
          },
        ]"
      >
        <el-radio v-model="haveOrgParent" :label="1"
          >有上级单位（总公司）</el-radio
        >
        <el-input
          v-show="haveOrgParent == 1"
          v-model="form.orgParent"
          placeholder="填写类似于该单位的总公司名称"
        ></el-input>
        <el-radio v-model="haveOrgParent" :label="0"
          >无上级单位（总公司）</el-radio
        >
      </el-form-item> -->
      <el-form-item label="地区" label-position="left">
        <el-radio v-model="form.orgRegion" label="东部地区"
          >东部地区</el-radio
        >
        <el-radio v-model="form.orgRegion" label="中部地区"
          >中部地区</el-radio
        >
        <el-radio v-model="form.orgRegion" label="西部地区">西部地区</el-radio>
        <el-button type="text" @click="showRegion">查看地区划分</el-button>
      </el-form-item>
      <el-form-item label="国家基层单位类别" label-position="left">
        <el-radio v-model="form.grassrootType" :label="1">城镇社区</el-radio>
        <el-radio v-model="form.grassrootType" :label="2">个体</el-radio>
        <el-radio v-model="form.grassrootType" :label="3"
          >艰苦行业事业单位</el-radio
        >
        <el-radio v-model="form.grassrootType" :label="4">农村建制村</el-radio>
        <el-radio v-model="form.grassrootType" :label="5"
          >县及县以下企业单位</el-radio
        >
        <el-radio v-model="form.grassrootType" :label="6"
          >县及县以下事业单位</el-radio
        >
        <el-radio v-model="form.grassrootType" :label="0">都不是</el-radio>
      </el-form-item>

      <el-form-item label="国防军工" label-position="left">
        <el-radio v-model="form.defenseMilitary" :label="1">是</el-radio>
        <el-radio v-model="form.defenseMilitary" :label="0">否</el-radio>
      </el-form-item>
      <el-form-item label="重点院校和科研单位" label-position="left">
        <el-radio v-model="form.collegeInstitute" :label="1">是</el-radio>
        <el-radio v-model="form.collegeInstitute" :label="0">否</el-radio>
      </el-form-item>
      <el-form-item label="重点国有企业" label-position="left">
        <el-radio v-model="form.keyNational" :label="1">是</el-radio>
        <el-radio v-model="form.keyNational" :label="0">否</el-radio>
      </el-form-item>
      <el-form-item label="重要医疗机构" label-position="left">
        <el-radio v-model="form.keyMedical" :label="1">是</el-radio>
        <el-radio v-model="form.keyMedical" :label="0">否</el-radio>
      </el-form-item>
      <el-form-item label="重点金融单位" label-position="left">
        <el-radio v-model="form.keyFinancial" :label="1">是</el-radio>
        <el-radio v-model="form.keyFinancial" :label="0">否</el-radio>
      </el-form-item>
      <el-form-item label="主流新闻媒体" label-position="left">
        <el-radio v-model="form.keyMedia" :label="1">是</el-radio>
        <el-radio v-model="form.keyMedia" :label="0">否</el-radio>
      </el-form-item>
      <el-form-item label="党政机关（非选调生）" label-position="left">
        <el-radio v-model="form.office" :label="1">是</el-radio>
        <el-radio v-model="form.office" :label="0">否</el-radio>
      </el-form-item>
      <el-form-item label="核心软件" label-position="left">
        <el-radio v-model="form.keySoftware" :label="1">是</el-radio>
        <el-radio v-model="form.keySoftware" :label="0">否</el-radio>
      </el-form-item>
      <el-form-item label="信息安全" label-position="left">
        <el-radio v-model="form.informSafety" :label="1">是</el-radio>
        <el-radio v-model="form.informSafety" :label="0">否</el-radio>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
        <el-button type="plain" @click="handleCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addOrganization, editOrganization, getOrganization } from '@/api/company.js'
export default ({
  data() {
    return {
      form: {
        id:"",
        orgName: "",
        //orgParent: "",
        orgRegion: "东部地区",
        grassrootType: 1,
        defenseMilitary: 1,
        collegeInstitute: 1,
        keyNational: 1,
        keyMedical: 1,
        keyFinancial: 1,
        keyMedia: 1,
        office: 1,
        keySoftware: 1,
        informSafety: 1
      },
      haveOrgParent: 1,
      isCreate: true,
    }
  },
  created() {
    //
    // let form = this.$route.params.dataObj
    // // console.log(form)
    // if (form) {
    //   this.form = form
    //   this.isCreate = false;
    // } else {
    //   this.isCreate = true;
    // }
    let id = this.$route.query.id
    if (id) {
      this.isCreate = false
      let data = { id: id }
      getOrganization(data).then(response => {
        this.form = response.data
      }).catch(err => {
        console.log(err)
      })
    } else {
      let orgParent = this.$route.query.orgParent;
      if(orgParent){//为上级单位添加下级单位信息
        this.form.orgParent = orgParent;
        console.log(orgParent);
      }
      this.isCreate = true
    }
  },
  watch: {
    haveOrgParent: function (val) {
      if (val == 1) {//有上级单位或总公司,让自己填
        this.form.orgParent = "";
      } else if (val == 0) {//没有上级单位或公司，就上级单位或总公司名字就是本单位的名字
        this.form.orgParent = this.form.orgName;
      }
    }
  },
  methods: {
    showRegion() {
      this.$notify({
        title: '地区划分',
        message: '<span style="font-size:14px"><span style="font-weight:bold">东部地区：</span>北京、天津、河北、辽宁、上海、江苏、浙江、福建、山东、广东、海南</span><br/><br/><span style="font-size:14px"><span style="font-weight:bold">中部地区：</span>山西、吉林、黑龙江、安徽、江西、河南、湖北、湖南</span><br/><br/><span style="font-size:14px"><span style="font-weight:bold">西部地区：</span>四川、重庆、贵州、云南、西藏、陕西、甘肃、青海、宁夏、新疆、广西、内蒙古</span>',
        dangerouslyUseHTMLString: true,
        duration: 0
      });
    },
    handleSubmit() {
      //判断是否符合要求
      this.$refs['form'].validate((valid) => {
        if (valid) {
          //上传给后端
          if (this.isCreate) {
            //新建上传
            let data = this.form
            addOrganization(data).then(response => {
              this.$message({
                type: 'success',
                message: '成功添加单位: ' + this.form.orgName + " 的信息"
              })
              // 调用全局挂载的方法
              this.$store.dispatch('tagsView/delView', this.$route)
              // 返回上一步路由
              this.$router.push('/company/index')
            }).catch(err => {
              console.log(err)
            })
          } else {
            //编辑上传
            let data = this.form
            editOrganization(data).then(response => {
              this.$message({
                type: 'success',
                message: '成功编辑单位: ' + this.form.orgName + ' 的信息'
              })
              // 调用全局挂载的方法
              this.$store.dispatch('tagsView/delView', this.$route)
              // 返回上一步路由
              this.$router.push('/company/index')
            }).catch(err => {
              console.log(err)
            })
          }
        } else {
          //提示有问题
          this.$message({
            message: '有必要信息未填写，请填写完整~',
            type: 'error'
          })
        }
      });

    },
    handleCancel() {
      this.$confirm('此操作将放弃表单所有内容，确定取消嘛？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用全局挂载的方法
        this.$store.dispatch('tagsView/delView', this.$route)
        // 返回上一步路由
        this.$router.push('/company/index')
      }).catch(() => {
        //do nothing
      });
    }
  }
})
</script>
